package com.han.shadow.customerapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.bean.Order;
import com.han.shadow.customerapp.http.Connector;
import com.han.shadow.customerapp.mystatic.MyScreen;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.RunnableFuture;

public class CheckoutActivity extends Activity {



    private EditText edit_ck_note = null;//备注
    private LinearLayout ll_ck_1 = null;//订单信息显示 Viewgroup
    private TextView text_ck_business = null;//商家
    private TextView text_ck_info = null;//总价 详情


    private List<Goods> goodses = null;
    private Map<String,Integer> goodsCart = null;
    private float allPrice = 0;
    private String bID = "";
    private String bName = "";


    private Order order = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkout);


        initValue();
        initCtrls();


    }

    public void initValue(){
        goodses = (ArrayList<Goods>) getIntent().getSerializableExtra("goodses");
        goodsCart = (Map<String,Integer>) getIntent().getSerializableExtra("goodsCart");
        allPrice = getIntent().getFloatExtra("allPrice",0);
        bID = getIntent().getStringExtra("bID");
        bName = getIntent().getStringExtra("bName");

        order = new Order();

        order.setBid(bID);
        order.setCid(MyScreen.cid);
        order.setOid("");
        order.setNote("");
        order.setOrderTime("");
        order.setStatus(-1);
        order.setGoodsIDs(goodsCart);
    }

    public void initCtrls(){
        edit_ck_note = (EditText)findViewById(R.id.edit_ck_note);//备注
        ll_ck_1 = (LinearLayout)findViewById(R.id.ll_ck_1);//订单信息显示 Viewgroup
        text_ck_business = (TextView)findViewById(R.id.text_ck_business);
        text_ck_info = (TextView)findViewById(R.id.text_ck_info);//总价 详情


        text_ck_business.setText("商家"+bName);
        text_ck_info.setText("总价" + String.valueOf(allPrice) + "元");


        for(Map.Entry<String,Integer> entry:goodsCart.entrySet()) {
            for(Goods goods:goodses){
                if(goods.getGid().equals(entry.getKey())){
                    TextView view = new TextView(this);
                    view.setText(goods.getName()+"        x"+entry.getValue()+"    "+(float)(goods.getPrice()/10)*entry.getValue()+"元");
                    view.setTextSize(20);
                    ll_ck_1.addView(view);
                }
            }

        }


    }


    public void confirm(View v){

        long time = System.currentTimeMillis();
        Date d1=new Date(time);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String t1=format.format(d1);
        order.setNote(edit_ck_note.getText().toString());
        order.setOrderTime(t1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Connector connector = new Connector();
                connector.checkout(order);
            }
        }).start();

        this.finish();

    }


}
